import 'dart:async';

import 'package:flutter/material.dart';

class Alert {
  Completer? _completer;

  BuildContext context;

  Alert(this.context);

  Future<bool> showWarning(String title, String message) async {
    _completer = Completer();
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(title),
          content: Text(message),
          actions: [
            TextButton(
              onPressed: _didTapCancel,
              child: const Text("取消"),
            ),
            TextButton(
              onPressed: _didTapConfirm,
              style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all<Color>(
                  Theme.of(context).primaryColor,
                ),
              ),
              child: Text(
                "确认",
                style: TextStyle(
                  color: Theme.of(context).colorScheme.onPrimary,
                ),
              ),
            ),
          ],
        );
      },
    );
    bool result = await _completer!.future;
    return result;
  }

  void _didTapCancel() {
    Navigator.pop(context);
    _completer?.complete(false);
  }

  void _didTapConfirm() {
    Navigator.pop(context);
    _completer?.complete(true);
  }
}
